package top.xia17.plugins.demo.config;

import cn.hutool.core.text.StrFormatter;
import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.database.mybatis.exception.EntityNotFoundException;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;
import top.xia17.plugins.fast.common.exceptions.VerifyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * 全局异常捕捉
 *
 * @author xia17
 * @date 2020/7/23 17:30
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    private Back<String> sendError(String msg) {
        return Back.error(msg);
    }

    private Back<String> sendError(String msg , Throwable e){
        Back<String> error = Back.error(msg);
        return error.setExtra(e.getMessage());
    }


    /**
     * 处理所有不可知的异常
     */
    @ExceptionHandler(Throwable.class)
    public Back<String> exception(Throwable e) {
        e.printStackTrace();
        log.error("系统未知异常", e);
        return sendError("系统异常",e);
    }


    /**
     * 处理已知的异常
     */
    @ExceptionHandler(BadRequestException.class)
    public Back<String> badRequestException(BadRequestException e) {
        log.error("自定义异常:{}", e.getMessage());
        StackTraceElement[] stackTrace = e.getStackTrace();
        if (stackTrace != null && stackTrace.length > 0) {
            log.error(stackTrace[0].toString());
        }
        return sendError(e.getMessage());
    }

    /**
     * 处理参数异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Back<String> constraintViolationException(ConstraintViolationException e) {
        // 解析验证错误信息
        String msg = e.getConstraintViolations().stream()
                .map(ConstraintViolation::getMessageTemplate)
                .collect(Collectors.joining(","));
        String result = StrFormatter.format("参数异常[{}]", msg);
        log.error("请求参数异常,{}", result);
        log.error("请求参数异常", e);
        return sendError(result);
    }

    /**
     * 处理参数异常
     */
    @ExceptionHandler(BindException.class)
    public Back<String> bindException(BindException e) {
        // 解析验证错误信息
        String msg = e.getFieldErrors().stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.joining(","));
        String result = StrFormatter.format("参数异常[{}]", msg);
        log.error("请求参数异常,{}", result);
        log.error("请求参数异常", e);
        return sendError(result);
    }


    /**
     * 数据验证异常
     */
    @ExceptionHandler(VerifyException.class)
    public Back<String> verifyException(VerifyException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
        return sendError(e.getMessage());
    }

    /**
     * 数据验证异常
     */
    @ExceptionHandler(EntityNotFoundException.class)
    public Back<String> entityNotFoundException(EntityNotFoundException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
        return sendError("数据异常");
    }


}
